package com.nosnow.edu.controller;

import com.nosnow.common.utils.R;
import com.nosnow.edu.entity.EduVideo;
import com.nosnow.edu.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author bingheng
 * @since 2020-04-04
 */
@Api(description = "课程小节表")
@RestController
@RequestMapping("/edu/video")
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @ApiOperation(value = "添加小节")
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        boolean flog = eduVideoService.save(eduVideo);
        if(flog) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据Id小节查询数据")
    @GetMapping("getVideoInfo/{id}")
    public R getVideoInfo(@PathVariable String id) {
        EduVideo eduVideo = eduVideoService.getById(id);
        return R.ok().data("eduVideo", eduVideo);
    }

    @ApiOperation(value = "根据Id修改小节")
    @PostMapping("modifyVideo")
    public R modifyVideo(@RequestBody EduVideo eduVideo) {
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }

    @ApiOperation(value = "根据Id删除小节")
    @DeleteMapping("removeVideo/{id}")
    public R removeVideo(@PathVariable String id) {
        // 1. 删除阿里云端视频
        eduVideoService.removeVideoByVideoId(id);

        // 2. 删除小节
        eduVideoService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value="根据小节Id将视频Id置空")
    @GetMapping("modifyVideoSourceIdByVideoId/{id}")
    public R modifyVideoSourceIdByVideoId(@PathVariable String id) {
        eduVideoService.updateVideoById(id);
        return R.ok();
    }

}

